package javolution.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.RandomAccess;
import javax.realtime.MemoryArea;
import javolution.context.ObjectFactory;
import javolution.context.PersistentContext;
import javolution.lang.MathLib;
import javolution.lang.Reusable;
import javolution.util.FastCollection;

/* loaded from: classes2.dex */
public class FastTable<E> extends FastCollection<E> implements List<E>, Reusable, RandomAccess {
    private static final int B0 = 4;
    private static final int B1 = 10;
    private static final int C0 = 16;
    private static final int C1 = 1024;
    private static final int M1 = 1023;
    private static final long serialVersionUID = 1;
    private transient int _capacity;
    private transient E[][] _high;
    private transient E[] _low;
    private transient int _size;
    private transient FastComparator<? super E> _valueComparator;
    private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastTable.1
        @Override // javolution.context.ObjectFactory
        public void cleanup(Object obj) {
            ((FastTable) obj).reset();
        }

        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new FastTable();
        }
    };
    private static final Object[] NULL_BLOCK = new Object[1024];
    static volatile int ONE_VOLATILE = 1;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static final class FastTableIterator implements ListIterator {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastTable.FastTableIterator.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public void cleanup(Object obj) {
                FastTableIterator fastTableIterator = (FastTableIterator) obj;
                fastTableIterator._table = null;
                fastTableIterator._low = null;
                fastTableIterator._high = null;
            }

            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new FastTableIterator();
            }
        };
        private int _currentIndex;
        private int _end;
        private Object[][] _high;
        private Object[] _low;
        private int _nextIndex;
        private int _start;
        private FastTable _table;

        private FastTableIterator() {
        }

        public static FastTableIterator valueOf(FastTable fastTable, int i, int i2, int i3) {
            FastTableIterator fastTableIterator = (FastTableIterator) FACTORY.object();
            fastTableIterator._table = fastTable;
            fastTableIterator._start = i2;
            fastTableIterator._end = i3;
            fastTableIterator._nextIndex = i;
            fastTableIterator._low = fastTable._low;
            fastTableIterator._high = fastTable._high;
            fastTableIterator._currentIndex = -1;
            return fastTableIterator;
        }

        @Override // java.util.ListIterator
        public void add(Object obj) {
            FastTable fastTable = this._table;
            int i = this._nextIndex;
            this._nextIndex = i + 1;
            fastTable.add(i, obj);
            this._end++;
            this._currentIndex = -1;
        }

        @Override // java.util.ListIterator, java.util.Iterator
        public boolean hasNext() {
            return this._nextIndex != this._end;
        }

        @Override // java.util.ListIterator
        public boolean hasPrevious() {
            return this._nextIndex != this._start;
        }

        @Override // java.util.ListIterator, java.util.Iterator
        public Object next() {
            int i = this._nextIndex;
            if (i != this._end) {
                if (i < 1024) {
                    Object[] objArr = this._low;
                    this._nextIndex = i + 1;
                    return objArr[i];
                }
                Object[] objArr2 = this._high[i >> 10];
                this._nextIndex = i + 1;
                return objArr2[i & 1023];
            }
            throw new NoSuchElementException();
        }

        @Override // java.util.ListIterator
        public int nextIndex() {
            return this._nextIndex;
        }

        @Override // java.util.ListIterator
        public Object previous() {
            int i = this._nextIndex;
            if (i != this._start) {
                int i2 = i - 1;
                this._nextIndex = i2;
                this._currentIndex = i2;
                return i2 < 1024 ? this._low[i2] : this._high[i2 >> 10][i2 & 1023];
            }
            throw new NoSuchElementException();
        }

        @Override // java.util.ListIterator
        public int previousIndex() {
            return this._nextIndex - 1;
        }

        @Override // java.util.ListIterator, java.util.Iterator
        public void remove() {
            int i = this._currentIndex;
            if (i < 0) {
                throw new IllegalStateException();
            }
            this._table.remove(i);
            this._end--;
            int i2 = this._currentIndex;
            int i3 = this._nextIndex;
            if (i2 < i3) {
                this._nextIndex = i3 - 1;
            }
            this._currentIndex = -1;
        }

        @Override // java.util.ListIterator
        public void set(Object obj) {
            int i = this._currentIndex;
            if (i < 0) {
                throw new IllegalStateException();
            }
            this._table.set(i, obj);
        }
    }

    /* loaded from: classes2.dex */
    private static final class SubTable extends FastCollection implements List, RandomAccess {
        private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.util.FastTable.SubTable.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public void cleanup(Object obj) {
                ((SubTable) obj)._table = null;
            }

            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new SubTable();
            }
        };
        private int _offset;
        private int _size;
        private FastTable _table;

        private SubTable() {
        }

        public static SubTable valueOf(FastTable fastTable, int i, int i2) {
            SubTable subTable = (SubTable) FACTORY.object();
            subTable._table = fastTable;
            subTable._offset = i;
            subTable._size = i2;
            return subTable;
        }

        @Override // java.util.List
        public void add(int i, Object obj) {
            throw new UnsupportedOperationException("Insertion not supported, thread-safe collections.");
        }

        @Override // java.util.List
        public boolean addAll(int i, Collection collection) {
            throw new UnsupportedOperationException("Insertion not supported, thread-safe collections.");
        }

        @Override // javolution.util.FastCollection
        public void delete(FastCollection.Record record) {
            throw new UnsupportedOperationException("Deletion not supported, thread-safe collections.");
        }

        @Override // java.util.List
        public Object get(int i) {
            if (i < 0 || i >= this._size) {
                throw new IndexOutOfBoundsException("index: " + i);
            }
            return this._table.get(i + this._offset);
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record head() {
            return Index.valueOf(-1);
        }

        @Override // java.util.List
        public int indexOf(Object obj) {
            FastComparator<? super E> valueComparator = this._table.getValueComparator();
            int i = -1;
            do {
                i++;
                if (i >= this._size) {
                    return -1;
                }
            } while (!valueComparator.areEqual(obj, (Object) this._table.get(this._offset + i)));
            return i;
        }

        @Override // java.util.List
        public int lastIndexOf(Object obj) {
            FastComparator<? super E> valueComparator = this._table.getValueComparator();
            int i = this._size;
            do {
                i--;
                if (i < 0) {
                    return -1;
                }
            } while (!valueComparator.areEqual(obj, (Object) this._table.get(this._offset + i)));
            return i;
        }

        @Override // java.util.List
        public ListIterator listIterator() {
            return listIterator(0);
        }

        @Override // java.util.List
        public ListIterator listIterator(int i) {
            int i2;
            if (i >= 0 && i <= (i2 = this._size)) {
                FastTable fastTable = this._table;
                int i3 = this._offset;
                return FastTableIterator.valueOf(fastTable, i + i3, i3, i2 + i3);
            }
            throw new IndexOutOfBoundsException("index: " + i + " for table of size: " + this._size);
        }

        @Override // java.util.List
        public Object remove(int i) {
            throw new UnsupportedOperationException("Deletion not supported, thread-safe collections.");
        }

        @Override // java.util.List
        public Object set(int i, Object obj) {
            if (i < 0 || i >= this._size) {
                throw new IndexOutOfBoundsException("index: " + i);
            }
            return this._table.set(i + this._offset, obj);
        }

        @Override // javolution.util.FastCollection, java.util.Collection
        public int size() {
            return this._size;
        }

        @Override // java.util.List
        public List subList(int i, int i2) {
            if (i < 0 || i2 > this._size || i > i2) {
                throw new IndexOutOfBoundsException("fromIndex: " + i + ", toIndex: " + i2 + " for list of size: " + this._size);
            }
            return valueOf(this._table, this._offset + i, i2 - i);
        }

        @Override // javolution.util.FastCollection
        public FastCollection.Record tail() {
            return Index.valueOf(this._size);
        }

        @Override // javolution.util.FastCollection
        public Object valueOf(FastCollection.Record record) {
            return this._table.get(((Index) record).intValue() + this._offset);
        }
    }

    public FastTable() {
        this._valueComparator = FastComparator.DEFAULT;
        this._capacity = 16;
        this._low = (E[]) new Object[16];
        this._high = (E[][]) new Object[1];
        this._high[0] = this._low;
    }

    public FastTable(int i) {
        this();
        while (i > this._capacity) {
            increaseCapacity();
        }
    }

    public FastTable(String str) {
        this();
        new PersistentContext.Reference(str, this) { // from class: javolution.util.FastTable.2
            @Override // javolution.context.PersistentContext.Reference
            protected void notifyChange() {
                FastTable.this.clear();
                FastTable.this.addAll((Collection) ((FastList) get()));
            }
        };
    }

    public FastTable(Collection<? extends E> collection) {
        this(collection.size());
        addAll(collection);
    }

    static /* synthetic */ int access$012(FastTable fastTable, int i) {
        int i2 = fastTable._capacity + i;
        fastTable._capacity = i2;
        return i2;
    }

    static /* synthetic */ int access$060(FastTable fastTable, int i) {
        int i2 = fastTable._capacity << i;
        fastTable._capacity = i2;
        return i2;
    }

    private static boolean defaultEquals(Object obj, Object obj2) {
        if (obj == null) {
            if (obj2 != null) {
                return false;
            }
        } else if (obj != obj2 && !obj.equals(obj2)) {
            return false;
        }
        return true;
    }

    private void increaseCapacity() {
        MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.util.FastTable.3
            @Override // java.lang.Runnable
            public void run() {
                if (FastTable.this._capacity < 1024) {
                    FastTable.access$060(FastTable.this, 1);
                    Object[] objArr = new Object[FastTable.this._capacity];
                    System.arraycopy(FastTable.this._low, 0, objArr, 0, FastTable.this._low.length);
                    FastTable.this._low = objArr;
                    FastTable.this._high[0] = objArr;
                    return;
                }
                int i = FastTable.this._capacity >> 10;
                if (i >= FastTable.this._high.length) {
                    Object[][] objArr2 = new Object[FastTable.this._high.length * 2];
                    System.arraycopy(FastTable.this._high, 0, objArr2, 0, FastTable.this._high.length);
                    FastTable.this._high = objArr2;
                }
                FastTable.this._high[i] = new Object[1024];
                FastTable.access$012(FastTable.this, 1024);
            }
        });
    }

    public static <E> FastTable<E> newInstance() {
        return (FastTable) FACTORY.object();
    }

    private int partition(int i, int i2, FastComparator fastComparator) {
        E e = get(i);
        int i3 = i;
        int i4 = i2;
        while (true) {
            if (fastComparator.compare(get(i3), e) > 0 || i3 >= i2) {
                while (fastComparator.compare(get(i4), e) > 0 && i4 > i) {
                    i4--;
                }
                if (i3 < i4) {
                    E e2 = get(i3);
                    set(i3, get(i4));
                    set(i4, e2);
                }
                if (i4 <= i3) {
                    set(i, get(i4));
                    set(i4, e);
                    return i4;
                }
            } else {
                i3++;
            }
        }
    }

    private void quicksort(int i, int i2, FastComparator fastComparator) {
        if (i < i2) {
            int partition = partition(i, i2, fastComparator);
            quicksort(i, partition - 1, fastComparator);
            quicksort(partition + 1, i2, fastComparator);
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException {
        setValueComparator((FastComparator) objectInputStream.readObject());
        int readInt = objectInputStream.readInt();
        int i = 16;
        while (true) {
            this._capacity = i;
            int i2 = this._capacity;
            if (i2 >= this._size || i2 >= 1024) {
                break;
            }
            i = i2 << 1;
        }
        this._low = (E[]) new Object[this._capacity];
        this._high = (E[][]) new Object[1];
        this._high[0] = this._low;
        for (int i3 = 0; i3 < readInt; i3++) {
            addLast(objectInputStream.readObject());
        }
    }

    public static void recycle(FastTable fastTable) {
        FACTORY.recycle(fastTable);
    }

    private void shiftLeft(int i, int i2) {
        while (i < this._size) {
            int i3 = i - i2;
            E[][] eArr = this._high;
            eArr[i3 >> 10][i3 & 1023] = eArr[i >> 10][i & 1023];
            i++;
        }
    }

    private void shiftRight(int i, int i2) {
        int i3;
        while (true) {
            i3 = this._size;
            if (i3 + i2 < this._capacity) {
                break;
            }
            increaseCapacity();
        }
        while (true) {
            i3--;
            if (i3 < i) {
                return;
            }
            int i4 = i3 + i2;
            E[][] eArr = this._high;
            eArr[i4 >> 10][i4 & 1023] = eArr[i3 >> 10][i3 & 1023];
        }
    }

    private void writeObject(ObjectOutputStream objectOutputStream) throws IOException {
        objectOutputStream.writeObject(getValueComparator());
        int i = this._size;
        objectOutputStream.writeInt(i);
        for (int i2 = 0; i2 < i; i2++) {
            objectOutputStream.writeObject(get(i2));
        }
    }

    @Override // java.util.List
    public final void add(int i, E e) {
        if (i < 0 || i > this._size) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        shiftRight(i, 1);
        this._high[i >> 10][i & 1023] = e;
        this._size += ONE_VOLATILE;
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final boolean add(E e) {
        if (this._size >= this._capacity) {
            increaseCapacity();
        }
        E[][] eArr = this._high;
        int i = this._size;
        eArr[i >> 10][i & 1023] = e;
        this._size = i + ONE_VOLATILE;
        return true;
    }

    @Override // java.util.List
    public final boolean addAll(int i, Collection<? extends E> collection) {
        if (i < 0 || i > this._size) {
            throw new IndexOutOfBoundsException("index: " + i);
        }
        int size = collection.size();
        shiftRight(i, size);
        Iterator<? extends E> it = collection.iterator();
        int i2 = i + size;
        while (i < i2) {
            this._high[i >> 10][i & 1023] = it.next();
            i++;
        }
        this._size += ONE_VOLATILE * size;
        return size != 0;
    }

    public final void addLast(E e) {
        add(e);
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final void clear() {
        int i = this._size;
        this._size = ONE_VOLATILE - 1;
        for (int i2 = 0; i2 < i; i2 += 1024) {
            int min = MathLib.min(i - i2, 1024);
            System.arraycopy(NULL_BLOCK, 0, this._high[i2 >> 10], 0, min);
        }
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final boolean contains(Object obj) {
        return indexOf(obj) >= 0;
    }

    @Override // javolution.util.FastCollection
    public final void delete(FastCollection.Record record) {
        remove(((Index) record).intValue());
    }

    @Override // java.util.List
    public final E get(int i) {
        if (i < this._size) {
            return i < 1024 ? this._low[i] : this._high[i >> 10][i & 1023];
        }
        throw new IndexOutOfBoundsException();
    }

    protected final int getCapacity() {
        return this._capacity;
    }

    public final E getFirst() {
        if (this._size != 0) {
            return this._low[0];
        }
        throw new NoSuchElementException();
    }

    public final E getLast() {
        int i = this._size;
        if (i != 0) {
            return get(i - 1);
        }
        throw new NoSuchElementException();
    }

    @Override // javolution.util.FastCollection
    public FastComparator<? super E> getValueComparator() {
        return this._valueComparator;
    }

    @Override // javolution.util.FastCollection
    public final FastCollection.Record head() {
        return Index.valueOf(-1);
    }

    @Override // java.util.List
    public final int indexOf(Object obj) {
        int i;
        FastComparator<? super E> valueComparator = getValueComparator();
        int i2 = 0;
        loop0: while (true) {
            int i3 = this._size;
            if (i2 >= i3) {
                return -1;
            }
            Object[] objArr = this._high[i2 >> 10];
            int min = MathLib.min(objArr.length, i3 - i2);
            i = 0;
            while (i < min) {
                if (valueComparator == FastComparator.DEFAULT) {
                    if (defaultEquals(obj, objArr[i])) {
                        break loop0;
                    }
                    i++;
                } else if (valueComparator.areEqual(obj, objArr[i])) {
                    break loop0;
                } else {
                    i++;
                }
            }
            i2 += min;
        }
        return i2 + i;
    }

    @Override // javolution.util.FastCollection, java.util.Collection, java.lang.Iterable
    public Iterator<E> iterator() {
        return FastTableIterator.valueOf(this, 0, 0, this._size);
    }

    /* JADX WARN: Code restructure failed: missing block: B:17:0x0033, code lost:
        r1 = r1 - r4;
     */
    @Override // java.util.List
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public final int lastIndexOf(java.lang.Object r8) {
        /*
            r7 = this;
            javolution.util.FastComparator r0 = r7.getValueComparator()
            int r1 = r7._size
            int r1 = r1 + (-1)
        L8:
            r2 = -1
            if (r1 < 0) goto L35
            E[][] r3 = r7._high
            int r4 = r1 >> 10
            r3 = r3[r4]
            r4 = r1 & 1023(0x3ff, float:1.434E-42)
            int r4 = r4 + 1
            r5 = r4
        L16:
            int r5 = r5 + r2
            if (r5 < 0) goto L33
            javolution.util.FastComparator<java.lang.Object> r6 = javolution.util.FastComparator.DEFAULT
            if (r0 != r6) goto L26
            r6 = r3[r5]
            boolean r6 = defaultEquals(r8, r6)
            if (r6 == 0) goto L16
            goto L2e
        L26:
            r6 = r3[r5]
            boolean r6 = r0.areEqual(r8, r6)
            if (r6 == 0) goto L16
        L2e:
            int r1 = r1 + r5
            int r1 = r1 - r4
            int r1 = r1 + 1
            return r1
        L33:
            int r1 = r1 - r4
            goto L8
        L35:
            return r2
        */
        throw new UnsupportedOperationException("Method not decompiled: javolution.util.FastTable.lastIndexOf(java.lang.Object):int");
    }

    @Override // java.util.List
    public ListIterator<E> listIterator() {
        return FastTableIterator.valueOf(this, 0, 0, this._size);
    }

    @Override // java.util.List
    public ListIterator<E> listIterator(int i) {
        int i2;
        if (i < 0 || i > (i2 = this._size)) {
            throw new IndexOutOfBoundsException();
        }
        return FastTableIterator.valueOf(this, i, 0, i2);
    }

    @Override // java.util.List
    public final E remove(int i) {
        E e = get(i);
        this._size -= ONE_VOLATILE;
        shiftLeft(i + 1, 1);
        E[][] eArr = this._high;
        int i2 = this._size;
        eArr[i2 >> 10][i2 & 1023] = null;
        return e;
    }

    public final E removeLast() {
        int i = this._size;
        if (i != 0) {
            this._size = i - ONE_VOLATILE;
            E[][] eArr = this._high;
            int i2 = this._size;
            E[] eArr2 = eArr[i2 >> 10];
            E e = eArr2[i2 & 1023];
            eArr2[i2 & 1023] = null;
            return e;
        }
        throw new NoSuchElementException();
    }

    public final void removeRange(int i, int i2) {
        int i3;
        if (i < 0 || i2 < 0 || i > i2 || i2 > (i3 = this._size)) {
            throw new IndexOutOfBoundsException();
        }
        int i4 = i2 - i;
        this._size = i3 - (ONE_VOLATILE * i4);
        shiftLeft(i, i4);
        int i5 = this._size;
        int i6 = i4 + i5;
        while (i5 < i6) {
            this._high[i5 >> 10][i5 & 1023] = null;
            i5++;
        }
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        clear();
        setValueComparator(FastComparator.DEFAULT);
    }

    @Override // java.util.List
    public final E set(int i, E e) {
        if (i < this._size) {
            E[] eArr = this._high[i >> 10];
            int i2 = i & 1023;
            E e2 = eArr[i2];
            eArr[i2] = e;
            return e2;
        }
        throw new IndexOutOfBoundsException();
    }

    public void setSize(int i) {
        while (this._size < i) {
            addLast(null);
        }
        while (this._size > i) {
            removeLast();
        }
    }

    public FastTable<E> setValueComparator(FastComparator<? super E> fastComparator) {
        this._valueComparator = fastComparator;
        return this;
    }

    @Override // javolution.util.FastCollection, java.util.Collection
    public final int size() {
        return this._size;
    }

    public final FastTable<E> sort() {
        int i = this._size;
        if (i > 1) {
            quicksort(0, i - 1, getValueComparator());
        }
        return this;
    }

    @Override // java.util.List
    public final List<E> subList(int i, int i2) {
        if (i < 0 || i2 > this._size || i > i2) {
            throw new IndexOutOfBoundsException("fromIndex: " + i + ", toIndex: " + i2 + " for list of size: " + this._size);
        }
        return SubTable.valueOf(this, i, i2 - i);
    }

    @Override // javolution.util.FastCollection
    public final FastCollection.Record tail() {
        return Index.valueOf(this._size);
    }

    public final void trimToSize() {
        while (true) {
            int i = this._capacity;
            if (i - this._size <= 1024) {
                return;
            }
            this._capacity = i - 1024;
            this._high[this._capacity >> 10] = null;
        }
    }

    @Override // javolution.util.FastCollection
    public List<E> unmodifiable() {
        return (List) super.unmodifiable();
    }

    @Override // javolution.util.FastCollection
    public final E valueOf(FastCollection.Record record) {
        return get(((Index) record).intValue());
    }
}
